/**------File Info----------------------------------------------
** File name:               Frame_Key.c
** Latest modified date:    2023-11-4
** Latest version:          V1.0.0
** Description:             按键控制
**
**------------------------------------------------------------
*/
#include "Frame.h"

/*************************************************************
** Function name:       KEYRunCycle
** Descriptions:        按键循环函数
*
** Input parameters:    gkey:对象名字
** Output parameters:   None
** Returned value:      None
** Remarks:             None
*************************************************************/
void KEYRunCycle(PKEYControl_Struct gkey)
	{
		switch (gkey->state)
		{
		case  KEY_STATE_IDLE:	//空闲状态
			if(gkey->Read_KEY() == gkey->active_level)		//如果检测到按键按下
			{
				gkey->state = KEY_STATE_PRESSED;		//进入按键按下状态
				gkey->press_time = FRAME_GETTIME();		//记录按键按下时间
			}
			break;
		case  KEY_STATE_PRESSED:	//按键按下状态
			if(gkey->Read_KEY() == gkey->active_level)		//如果检测到按键依然按下
			{
				if(FRAME_TIMEOUT(gkey->long_press_time, gkey->press_time))	//如果按键按下时间超过长按阈值
				{
					gkey->state = KEY_STATE_HOLD;		//进入按键长按状态
					gkey->event = KEY_EVENT_LONG_PRESS;		//触发长按事件
				}
			}
			else if(gkey->Read_KEY() != gkey->active_level)		//如果检测到按键松开
			{
			    if(!(FRAME_TIMEOUT(gkey->debounce_time, gkey->press_time)))	//如果按键按下时间没有超过去抖阈值
				{
					gkey->state = KEY_STATE_IDLE;		//进入按键空闲状态
					gkey->event = KEY_EVENT_NONE;		//按键事件为无
				}
				else 									//如果按键按下时间超过去抖阈值
				{	
					if(FRAME_TIMEOUT(gkey->double_click_time, gkey->release_time))	//如果按键抬起时间超过双击阈值->单击
					{
						gkey->state = KEY_STATE_IDLE;		//进入按键松开状态
						gkey->release_time = FRAME_GETTIME();		//记录按键松开时间
						gkey->event = KEY_EVENT_CLICK;		//按键事件为单击
						gkey->on_click();		//执行单击事件回调函数
					}
					else
					{
						gkey_state = KEY_STATE_IDLE;		//进入按键空闲状态
						gkey->event = KEY_EVENT_DOUBLE_CLICK;		//按键事件为双击
					}
					
				}
			}
		case  KEY_STATE_RELEASED:	//按键松开状态
			
			break;
		case  KEY_STATE_HOLD:		//按键长按状态
			if(gkey->Read_KEY() != gkey->active_level)		//如果检测到按键松开
			{
				gkey->state = KEY_STATE_IDLE;		//进入按键空闲状态
				gkey->event = KEY_EVENT_NONE;		//按键事件为无
			}
			else
			{
				gkey->event = KEY_EVENT_LONG_PRESS;		//按键事件为长按
				gkey->on_long_press();		//执行长按回调函数
			}
			break;
		
		default:
			break;
		}
	
	}





































